#include "userMain.h"
#include "userUartRxCtrl.h"

extern uint8_t RxEndFlag;
extern uint8_t userRxBuffer[];
void userUartRxCtrlFunc(void)
{
    if(RxEndFlag==0) return ;
    RxEndFlag = 0;
    switch(userRxBuffer[3])
    {
        case 0x01:
        {
            userLedCtrlFunc(userRxBuffer[4]);
            break;
        }
        case 0x02:
        {
            userSetTimeFunc();
            break;
        }
        case 0x03:
        {
            userGetTimeFunc();
            break;
        }
        case 0x04:
        {
            userIRctrlFunc();
            break;
        }
        case 0x05:
        {
            userIRlearnModeStartFunc();
            break;
        }
        default:
        {
            break;
        }
    }  
}


extern uint8_t userIR_Code[];
extern uint8_t userIR_Decode[];
extern uint16_t userIrDecodeLength;
extern uint8_t userIrDecodeErrorFlag;
extern uint8_t uBmaDetectFlag;
void userIRctrlFunc(void)           //receive IR code and send IR control signal
{
    uint16_t user_i,userIrDataLen;
    userIrDataLen = (uint16_t)userRxBuffer[1]<< 8 ;
    userIrDataLen += userRxBuffer[2]-8;
    userIrDecodeErrorFlag = 0;
    if(userRxBuffer[6]==1)
    {
        for(user_i=0;user_i<userIrDataLen;user_i++)
            userIR_Code[user_i] = userRxBuffer[user_i+7];
        userIR_Code[0] = userIrDataLen;
        userIrDecodeFunc();  
        uBmaDetectFlag = 1;
        userIrSendFunc();
        uBmaDetectFlag = 0;
    }
    else if((userRxBuffer[6]==0))
    {
        userIrDecodeLength = userIrDataLen;
        for(user_i=0;user_i<userIrDataLen;user_i++)
            userIR_Decode[user_i] = userRxBuffer[user_i+9];
        uBmaDetectFlag = 1;
        userIrSendFunc();
        uBmaDetectFlag = 0;
    }
}

uint8_t userIrDstAddrH,userIrDstAddrL;
void userIRlearnModeStartFunc(void)
{
    userIrDstAddrH = userRxBuffer[4];
    userIrDstAddrL = userRxBuffer[5];
    userIrRxStart();
}

extern uint8_t PCF8563_TimeInitValue[];
void userSetTimeFunc(void)
{
    uint16_t userYear;
    PCF8563_TimeInitValue[0] = (userRxBuffer[10]/10) << 4;
    PCF8563_TimeInitValue[0] += userRxBuffer[10]%10 ;           //SECOND
    PCF8563_TimeInitValue[1] = (userRxBuffer[9]/10) << 4;
    PCF8563_TimeInitValue[1] += userRxBuffer[9]%10 ;             //MINUTE
    PCF8563_TimeInitValue[2] = (userRxBuffer[8]/10)<< 4;
    PCF8563_TimeInitValue[2] += userRxBuffer[8]%10;              //HOUR
    PCF8563_TimeInitValue[3] = (userRxBuffer[7]/10)<< 4 ;
    PCF8563_TimeInitValue[3] += userRxBuffer[7]%10 ;             //DAY
    PCF8563_TimeInitValue[5] = (userRxBuffer[6]/10)<< 4 ;
    PCF8563_TimeInitValue[5] += userRxBuffer[6]%10 ;             //MONTH
    userYear = userRxBuffer[4];
    userYear = userYear << 8 ;
    userYear += userRxBuffer[5];
    userYear = userYear % 100;
    PCF8563_TimeInitValue[6] = userYear/10*16 + userYear%10;
    userPCF8563_Initial();
}

extern uint8_t userTimeBuff[];
extern uint8_t userTxBuf[];
void userGetTimeFunc(void)
{
     uint16_t userYear = 2000;
     userGetTimeFromPCF8563();
     userYear += (userTimeBuff[6]>>4)*10;
     userYear += userTimeBuff[6]&0x0F;
     userTxBuf[4] = userYear>>8;
     userTxBuf[5] = userYear & 0x00FF;
     userTxBuf[6] = (userTimeBuff[5]>>4)*10;
     userTxBuf[6] += userTimeBuff[5]&0x0F;               //MONTH
     userTxBuf[7] = (userTimeBuff[3]>>4)*10;
     userTxBuf[7] += userTimeBuff[3]&0x0F;               //DAY
     userTxBuf[8] = (userTimeBuff[2]>>4)*10;
     userTxBuf[8] += userTimeBuff[2]&0x0F;               //HOUR
     userTxBuf[9] = (userTimeBuff[1]>>4)*10; 
     userTxBuf[9] += userTimeBuff[1]&0x0F;               //MIN
     userTxBuf[10] = (userTimeBuff[0]>>4)*10;
     userTxBuf[10] += userTimeBuff[0]&0x0F;              //SECOND
     userSendTimeFunc();
}






